import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SecurityLog } from './entities/security-log.entity';
import { SecurityLogService } from './services/security-log.service';
import { SecurityLogController } from './controllers/security-log.controller';
import { SecurityLogInterceptor } from './interceptors/security-log.interceptor';
import { SecurityLogger } from './utils/security-logger.util';
import { SecurityEventEmitter } from './events/security-event.emitter';
import { SecurityEventListener } from './listeners/security-event.listener';
import { SecurityEventMiddleware } from './middleware/security-event.middleware';

@Module({
  imports: [
    TypeOrmModule.forFeature([SecurityLog]),
    EventEmitterModule.forRoot({
      // 全局事件发射器配置
      wildcard: false,
      delimiter: '.',
      maxListeners: 20,
      verboseMemoryLeak: false,
      ignoreErrors: false,
    }),
  ],
  controllers: [SecurityLogController],
  providers: [
    SecurityLogService,
    SecurityLogInterceptor,
    SecurityLogger,
    SecurityEventEmitter,
    SecurityEventListener,
    SecurityEventMiddleware,
  ],
  exports: [
    SecurityLogService,
    SecurityLogInterceptor,
    SecurityLogger,
    SecurityEventEmitter,
  ],
})
export class SecurityLogModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(SecurityEventMiddleware)
      .forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}
